﻿using System;
using System.Collections.Generic;
using SharpSqlAnalyzer.nodes;
using SharpSqlAnalyzer.pp.para.styleenums;
using SharpSqlAnalyzer.pp.utils;

namespace SharpSqlAnalyzer.pp.processor.type.comm
{
	// Token: 0x0200024B RID: 587
	public class AlignAliasProcessor : AbstractProcessor
	{
		// Token: 0x06001319 RID: 4889 RVA: 0x0031C88C File Offset: 0x0031AA8C
		public override void afterProcess(TParseTreeNode node)
		{
			if (!(node is TResultColumnList) && !(node is TParameterDeclarationList))
			{
				return;
			}
			bool parameter = this.getParameter<bool>(typeof(bool));
			TAlignStyle parameter2 = this.getParameter<TAlignStyle>(typeof(TAlignStyle), 1);
			if (!parameter || parameter2 == TAlignStyle.AsWrapped)
			{
				return;
			}
			TParseTreeNodeList tparseTreeNodeList = node as TParseTreeNodeList;
			int num = -1;
			IDictionary<string, int?> dictionary = new Dictionary<string, int?>();
			int i = 0;
			while (i < tparseTreeNodeList.size())
			{
				TParseTreeNode element = tparseTreeNodeList.getElement(i);
				int posinlist = element.startToken.posinlist;
				int num2 = -1;
				if (element is TResultColumn)
				{
					num2 = this.getEndPos((TResultColumn)element);
					if (num2 != -1)
					{
						goto IL_A8;
					}
				}
				else
				{
					if (!(element is TParameterDeclaration))
					{
						goto IL_A8;
					}
					num2 = this.getEndPos((TParameterDeclaration)element);
					if (num2 != -1)
					{
						goto IL_A8;
					}
				}
				IL_EA:
				i++;
				continue;
				IL_A8:
				int num3 = SourceTokenOperator.curColumnNumberVT(element.startToken.container, num2);
				dictionary[posinlist.ToString() + "_" + num2.ToString()] = new int?(num3);
				if (num3 >= num)
				{
					num = num3;
					goto IL_EA;
				}
				goto IL_EA;
			}
			int j = 0;
			while (j < tparseTreeNodeList.size())
			{
				TParseTreeNode element2 = tparseTreeNodeList.getElement(j);
				int posinlist2 = element2.startToken.posinlist;
				int num4 = -1;
				if (element2 is TResultColumn)
				{
					num4 = this.getEndPos((TResultColumn)element2);
					if (num4 != -1)
					{
						goto IL_15C;
					}
				}
				else
				{
					if (!(element2 is TParameterDeclaration))
					{
						goto IL_15C;
					}
					num4 = this.getEndPos((TParameterDeclaration)element2);
					if (num4 != -1)
					{
						goto IL_15C;
					}
				}
				IL_1B8:
				j++;
				continue;
				IL_15C:
				int value = dictionary[posinlist2.ToString() + "_" + num4.ToString()].Value;
				int num5 = num - value;
				if (num5 != 0)
				{
					TSourceToken needAddToken = SourceTokenOperator.createWhitespaceSourceToken(num5);
					SourceTokenOperator.addBefore(this.Option, element2.startToken.container.get(num4), needAddToken);
					goto IL_1B8;
				}
				goto IL_1B8;
			}
		}

		// Token: 0x0600131A RID: 4890 RVA: 0x0031CA64 File Offset: 0x0031AC64
		private int getEndPos(TResultColumn c)
		{
			if (c.AliasClause == null)
			{
				return -1;
			}
			int num = SourceTokenSearcher.LastIndexOf(c.startToken.container, c.startToken.posinlist, c.AliasClause.startToken.posinlist, "as");
			if (num == -1)
			{
				num = c.AliasClause.startToken.posinlist;
			}
			return num;
		}

		// Token: 0x0600131B RID: 4891 RVA: 0x0031CAC4 File Offset: 0x0031ACC4
		private int getEndPos(TParameterDeclaration c)
		{
			int num = SourceTokenSearcher.LastIndexOf(c.startToken.container, c.startToken.posinlist, c.endToken.posinlist, "as");
			if (num == -1)
			{
				num = c.endToken.posinlist;
			}
			return num;
		}
	}
}
